package com.ruoyi.sycs.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.sycs.domain.bo.FanganUseInfoBo;
import com.ruoyi.sycs.domain.vo.FanganUseInfoVo;
import com.ruoyi.sycs.domain.FanganUseInfo;
import com.ruoyi.sycs.mapper.FanganUseInfoMapper;
import com.ruoyi.sycs.service.IFanganUseInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 方案使用情况Service业务层处理
 *
 * @author zyb
 * @date 2024-10-17
 */
@RequiredArgsConstructor
@Service
public class FanganUseInfoServiceImpl implements IFanganUseInfoService {

    private final FanganUseInfoMapper baseMapper;

    /**
     * 查询方案使用情况
     */
    @Override
    public FanganUseInfoVo queryById(Long excelId){
        return baseMapper.selectVoById(excelId);
    }

    /**
     * 查询方案使用情况列表
     */
    @Override
    public TableDataInfo<FanganUseInfoVo> queryPageList(FanganUseInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<FanganUseInfo> lqw = buildQueryWrapper(bo);
        Page<FanganUseInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询方案使用情况列表
     */
    @Override
    public List<FanganUseInfoVo> queryList(FanganUseInfoBo bo) {
        LambdaQueryWrapper<FanganUseInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<FanganUseInfo> buildQueryWrapper(FanganUseInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<FanganUseInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, FanganUseInfo::getExcelId, bo.getExcelId());
        lqw.eq(bo.getFanganId() != null, FanganUseInfo::getFanganId, bo.getFanganId());
        return lqw;
    }

    /**
     * 新增方案使用情况
     */
    @Override
    public Boolean insertByBo(FanganUseInfoBo bo) {
        FanganUseInfo add = BeanUtil.toBean(bo, FanganUseInfo.class);
        validEntityBeforeSave(add);
        baseMapper.delete(new LambdaQueryWrapper<FanganUseInfo>().eq(FanganUseInfo::getExcelId, bo.getExcelId()));
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改方案使用情况
     */
    @Override
    public Boolean updateByBo(FanganUseInfoBo bo) {
        FanganUseInfo update = BeanUtil.toBean(bo, FanganUseInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(FanganUseInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除方案使用情况
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
